<template>
  <div class="chart-card">
    <div class="card-header">
      <h3 class="card-title">{{ title }}</h3>
      <div class="period-selector">
        <el-radio-group v-model="selectedPeriod" size="small">
          <el-radio-button label="week">周</el-radio-button>
          <el-radio-button label="month">月</el-radio-button>
          <el-radio-button label="year">年</el-radio-button>
        </el-radio-group>
      </div>
    </div>
    <div class="card-content">
      <div ref="chartContainer" class="chart-container"></div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted, watch } from 'vue'
import * as echarts from 'echarts'

const props = defineProps({
  title: {
    type: String,
    default: '文章趋势'
  },
  weekData: {
    type: Object,
    default: () => ({
      xAxis: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
      series: [5, 8, 12, 6, 9, 3, 7]
    })
  },
  monthData: {
    type: Object,
    default: () => ({
      xAxis: Array.from({length: 30}, (_, i) => (i + 1) + '日'),
      series: Array.from({length: 30}, () => Math.floor(Math.random() * 15))
    })
  },
  yearData: {
    type: Object,
    default: () => ({
      xAxis: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
      series: [30, 40, 35, 50, 49, 60, 70, 65, 80, 85, 90, 100]
    })
  }
})

const chartContainer = ref(null)
const selectedPeriod = ref('month')
let chart = null

const updateChart = () => {
  if (!chart) return
  
  let data
  switch (selectedPeriod.value) {
    case 'week':
      data = props.weekData
      break
    case 'month': 
      data = props.monthData
      break
    case 'year':
      data = props.yearData
      break
    default:
      data = props.monthData
  }
  
  chart.setOption({
    xAxis: {
      data: data.xAxis
    },
    series: [{
      data: data.series
    }]
  })
}

const initChart = () => {
  if (!chartContainer.value) return
  
  // 选择初始数据
  const initialData = selectedPeriod.value === 'week' ? props.weekData : 
                      selectedPeriod.value === 'year' ? props.yearData : props.monthData
  
  // 设置图表选项
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: initialData.xAxis,
      axisLine: {
        lineStyle: {
          color: '#ddd'
        }
      },
      axisLabel: {
        color: '#666',
        fontSize: 11
      }
    },
    yAxis: {
      type: 'value',
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      splitLine: {
        lineStyle: {
          type: 'dashed',
          color: '#eee'
        }
      },
      axisLabel: {
        color: '#666',
        fontSize: 11
      }
    },
    series: [
      {
        name: '文章数',
        type: 'line',
        smooth: true,
        symbol: 'circle',
        symbolSize: 6,
        data: initialData.series,
        itemStyle: {
          color: '#409EFF'
        },
        lineStyle: {
          width: 3,
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 1,
            y2: 0,
            colorStops: [{
              offset: 0, color: '#409EFF'
            }, {
              offset: 1, color: '#36CEE5'
            }]
          }
        },
        areaStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
              offset: 0, color: 'rgba(64, 158, 255, 0.3)'
            }, {
              offset: 1, color: 'rgba(64, 158, 255, 0.1)'
            }]
          }
        }
      }
    ]
  }

  // 如果图表已经存在，销毁它
  if (chart) {
    chart.dispose()
  }
  
  // 创建图表实例
  chart = echarts.init(chartContainer.value)
  // 应用选项
  chart.setOption(option)
  
  // 响应窗口大小变化
  window.addEventListener('resize', handleResize)
}

const handleResize = () => {
  if (chart) {
    chart.resize()
  }
}

// 监听周期变化
watch(() => selectedPeriod.value, () => {
  updateChart()
})

onMounted(() => {
  initChart()
})

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
  if (chart) {
    chart.dispose()
    chart = null
  }
})
</script>

<style scoped>
.chart-card {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  padding: 20px;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.card-title {
  font-size: 16px;
  font-weight: 600;
  color: #1f2329;
  margin: 0;
}

.period-selector {
  display: flex;
  gap: 8px;
}

.card-content {
  flex: 1;
  min-height: 300px;
}

.chart-container {
  width: 100%;
  height: 100%;
  min-height: 300px;
}
</style> 